﻿//-----------------------------------------------------------------------
// <copyright file="ClipboardModule.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace App.Modules.Clipboard
{
    using System.Collections.Generic;
    using App.Modules.Component;
    using App.Modules.Message;
    using App.Modules.Scene;
    using TileTool.Clipboard;
    using TileTool.DataBag;
    using TileTool.Editor.Command;
    using TileTool.Log;
    using TileTool.Message;
    using TileTool.PerfSpy;
    using TileTool.Scene;
    using TileTool.Scene.Entity;

    /// <summary>
    /// Application clipboard module.
    /// </summary>
    public partial class ClipboardModule : Module
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "ClipboardModule";

        /// <summary>
        /// Key for the info data bag.
        /// </summary>
        private const string InfoBagKey = "Info";

        /// <summary>
        /// Format to use for the object key.
        /// </summary>
        private const string ObjectKeyFormat = "Object-{0}";

        #endregion

        #region Fields - Private

        /// <summary>
        /// Clipboard interface for the application.
        /// </summary>
        private IClipboard clipboard = null;

        #endregion

        #region Methods - Public

        /// <summary>
        /// Called when the application is starting up.
        /// </summary>
        public override void Startup()
        {
            LogSystem.LogStartup(LogGroup, "Starting...");

            ComponentModule componentModule = GetModule<ComponentModule>();
            ClipboardFactoryComponentBase factory = componentModule.Get<ClipboardFactoryComponentBase>(ComponentAliases.Clipboard);

            LogSystem.LogStartup(LogGroup, "Allocating clipboard interface...");
            this.clipboard = factory.Allocate();

            base.Startup();
        }

        /// <summary>
        /// Called when the application is shutting down.
        /// </summary>
        public override void Shutdown()
        {
            base.Shutdown();

            this.clipboard = null;
        }

        /// <summary>
        /// Register for messages.
        /// </summary>
        public override void RegisterForMessages()
        {
            base.RegisterForMessages();

            MessageModule messageModule = GetModule<MessageModule>();
            MessageRouter router = messageModule.MessageRouter;

            router.RegisterForMessage<CommandIdMessage>(
                this.HandleCutCommandMessage,
                MessageHandlerPriority.Default,
                new CommandIdMessageFilter(EditorCommandIds.CutCommand));

            router.RegisterForMessage<CommandIdMessage>(
                this.HandleCopyCommandMessage,
                MessageHandlerPriority.Default,
                new CommandIdMessageFilter(EditorCommandIds.CopyCommand));

            router.RegisterForMessage<CommandIdMessage>(
                this.HandlePasteCommandMessage,
                MessageHandlerPriority.Default,
                new CommandIdMessageFilter(EditorCommandIds.PasteCommand));
        }

        /// <summary>
        /// Unregister for messages.
        /// </summary>
        public override void UnregisterForMessages()
        {
            base.UnregisterForMessages();

            MessageModule messageModule = GetModule<MessageModule>();
            MessageRouter router = messageModule.MessageRouter;

            router.UnregisterForMessage<CommandIdMessage>(this.HandleCutCommandMessage);
            router.UnregisterForMessage<CommandIdMessage>(this.HandleCopyCommandMessage);
            router.UnregisterForMessage<CommandIdMessage>(this.HandlePasteCommandMessage);
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Handle a cut command ID message.
        /// </summary>
        /// <param name="message">Command ID message to handle.</param>
        /// <returns>Returns if the message was handled or not.</returns>
        private MessageHandlerResult HandleCutCommandMessage(CommandIdMessage message)
        {
            return MessageHandlerResult.Ignored;
        }

        /// <summary>
        /// Handle a copy command ID message.
        /// </summary>
        /// <param name="message">Command ID message to handle.</param>
        /// <returns>Returns if the message was handled or not.</returns>
        private MessageHandlerResult HandleCopyCommandMessage(CommandIdMessage message)
        {
            using (new TimedBlock("SetClipboardDataToSelection"))
            {
                this.SetClipboardDataToSelection();
            }

            return MessageHandlerResult.Ignored;
        }

        /// <summary>
        /// Handle a paste command ID message.
        /// </summary>
        /// <param name="message">Command ID message to handle.</param>
        /// <returns>Returns if the message was handled or not.</returns>
        private MessageHandlerResult HandlePasteCommandMessage(CommandIdMessage message)
        {
            using (new TimedBlock("PasteClipboardData"))
            {
                this.PasteClipboardData();
            }
            
            return MessageHandlerResult.Ignored;
        }

        #endregion
    }
}
